﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpGen.Templates;
using ICeFramework.Utilities.Text;

namespace SharpGen.Controls
{
    public partial class FindReplace : UserControl
    {
        public event EventHandler Change;
        public event EventHandler Close;

        private StringReplacement _options;
        public StringReplacement Options 
        {
            get { return _options; }
            set 
            {
                _options = value;
                if (_options == null)
                {
                    _options = new StringReplacement();
                }
                SetControlState();
            } 
        }

        public FindReplace()
        {
            InitializeComponent();
            Options = new StringReplacement();
            Options.FindOptions = ICeFramework.Utilities.Text.FindOptions.Contains;
            titlePanel1.Close += new EventHandler(titlePanel1_Close);
            SetControlState();
        }

        private void SetControlState()
        {
            if (Options == null)
            {
                txtFind.Text = string.Empty;
                txtReplace.Text = string.Empty;
                txtPrefix.Text = string.Empty;
                txtSuffix.Text = string.Empty;
                beginWithToolStripMenuItem.Checked = false;
                containsToolStripMenuItem.Checked = false;
                endsWithToolStripMenuItem.Checked = false;
                matchCaseToolStripMenuItem.Checked = false;
                return;
            }

            txtFind.Text = Options.Find;
            txtReplace.Text = Options.Replace;
            txtPrefix.Text = Options.Prefix;
            txtSuffix.Text = Options.Suffix;
            beginWithToolStripMenuItem.Checked = (Options.FindOptions & FindOptions.Begin) > 0;
            containsToolStripMenuItem.Checked = (Options.FindOptions & FindOptions.Contains) > 0;
            endsWithToolStripMenuItem.Checked = (Options.FindOptions & FindOptions.End) > 0;
            matchCaseToolStripMenuItem.Checked = (Options.FindOptions & FindOptions.MatchCase) > 0;

            replaceTypeAllToolStripMenuItem.Checked = Options.ReplacementType == StringReplacementType.All;
            classNameToolStripMenuItem.Checked = Options.ReplacementType == StringReplacementType.ClassName;
            fileNameToolStripMenuItem.Checked = Options.ReplacementType == StringReplacementType.FileName;

            objectTypeAllToolStripMenuItem.Checked = Options.ObjectType == Enums.SqlObjectType.NA;
            tableToolStripMenuItem.Checked = Options.ObjectType == Enums.SqlObjectType.Table;
            viewToolStripMenuItem.Checked = Options.ObjectType == Enums.SqlObjectType.View;
            storedProcedureToolStripMenuItem.Checked = Options.ObjectType == Enums.SqlObjectType.StoredProcedure;

            SetContextLabels();
        }

        private void SetContextLabels()
        {
            List<string> optList = new List<string>();
            if ((Options.FindOptions & FindOptions.None) > 0) { optList.Add("All"); }
            if ((Options.FindOptions & FindOptions.Begin) > 0) { optList.Add("Begins with"); }
            if ((Options.FindOptions & FindOptions.Contains) > 0) { optList.Add("Contains"); }
            if ((Options.FindOptions & FindOptions.End) > 0) { optList.Add("Ends with"); }
            string opt = string.Format("[{0}]", string.Join(", ", optList.ToArray()));
            opt += (Options.FindOptions & FindOptions.MatchCase) > 0 ? ", Match Case" : string.Empty;
            lblOptions.Text = opt;

            switch (Options.ReplacementType)
            {
                case StringReplacementType.All:
                    lblReplaceType.Text = "All";
                    break;
                case StringReplacementType.ClassName:
                    lblReplaceType.Text = "Class Name";
                    break;
                case StringReplacementType.FileName:
                    lblReplaceType.Text = "File Name";
                    break;
                default:
                    lblReplaceType.Text = "";
                    break;
            }

            switch (Options.ObjectType)
            {
                case SharpGen.Enums.SqlObjectType.NA:
                    lblObjectType.Text = "All";
                    break;
                case SharpGen.Enums.SqlObjectType.Table:
                    lblObjectType.Text = "Table";
                    break;
                case SharpGen.Enums.SqlObjectType.View:
                    lblObjectType.Text = "View";
                    break;
                case SharpGen.Enums.SqlObjectType.StoredProcedure:
                    lblObjectType.Text = "Stored Procedure";
                    break;
                default:
                    lblObjectType.Text = "";
                    break;
            }
        }

        void titlePanel1_Close(object sender, EventArgs e)
        {
            OnClose();
        }

        public virtual void OnClose()
        {
            if (Close != null)
            {
                Close(this, EventArgs.Empty);
            }
        }

        public virtual void OnChange()
        {
            SetContextLabels();
            if (Change != null)
            {
                Change(this, EventArgs.Empty);
            }
        }

        private void beginWithToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (beginWithToolStripMenuItem.Checked)
            {
                Options.FindOptions = Options.FindOptions | FindOptions.Begin;
            }
            else
            {
                Options.FindOptions = Options.FindOptions & ~FindOptions.Begin;
            }
            OnChange();
        }

        private void containsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (containsToolStripMenuItem.Checked)
            {
                Options.FindOptions = Options.FindOptions | FindOptions.Contains;
            }
            else
            {
                Options.FindOptions = Options.FindOptions & ~FindOptions.Contains;
            }
            OnChange();
        }

        private void endsWithToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (endsWithToolStripMenuItem.Checked)
            {
                Options.FindOptions = Options.FindOptions | FindOptions.End;
            }
            else
            {
                Options.FindOptions = Options.FindOptions & ~FindOptions.End;
            }
            OnChange();
        }

        private void matchCaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (matchCaseToolStripMenuItem.Checked)
            {
                Options.FindOptions = Options.FindOptions | FindOptions.MatchCase;
            }
            else
            {
                Options.FindOptions = Options.FindOptions & ~FindOptions.MatchCase;
            }
            OnChange();
        }

        private void txtFind_TextChanged(object sender, EventArgs e)
        {
            Options.Find = txtFind.Text;
            OnChange();
        }

        private void txtReplace_TextChanged(object sender, EventArgs e)
        {
            Options.Replace = txtReplace.Text;
            OnChange();
        }

        private void txtPrefix_TextChanged(object sender, EventArgs e)
        {
            Options.Prefix = txtPrefix.Text;
            OnChange();
        }

        private void txtSuffix_TextChanged(object sender, EventArgs e)
        {
            Options.Suffix = txtSuffix.Text;
            OnChange();
        }

        private void replaceTypeAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            classNameToolStripMenuItem.Checked = !replaceTypeAllToolStripMenuItem.Checked;
            fileNameToolStripMenuItem.Checked = !replaceTypeAllToolStripMenuItem.Checked;
            if (replaceTypeAllToolStripMenuItem.Checked)
            {
                Options.ReplacementType = StringReplacementType.All;
            }
            OnChange();
        }

        private void classNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            replaceTypeAllToolStripMenuItem.Checked = !classNameToolStripMenuItem.Checked;
            fileNameToolStripMenuItem.Checked = !classNameToolStripMenuItem.Checked;
            if (classNameToolStripMenuItem.Checked)
            {
                Options.ReplacementType = StringReplacementType.ClassName;
            }
            OnChange();
        }

        private void fileNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            replaceTypeAllToolStripMenuItem.Checked = !fileNameToolStripMenuItem.Checked;
            classNameToolStripMenuItem.Checked = !fileNameToolStripMenuItem.Checked;
            if (fileNameToolStripMenuItem.Checked)
            {
                Options.ReplacementType = StringReplacementType.FileName;
            }
            OnChange();
        }

        private void objectTypeAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tableToolStripMenuItem.Checked = !objectTypeAllToolStripMenuItem.Checked;
            viewToolStripMenuItem.Checked = !objectTypeAllToolStripMenuItem.Checked;
            storedProcedureToolStripMenuItem.Checked = !objectTypeAllToolStripMenuItem.Checked;
            if (objectTypeAllToolStripMenuItem.Checked)
            {
                Options.ObjectType = Enums.SqlObjectType.NA;
            }
            OnChange();
        }

        private void tableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            objectTypeAllToolStripMenuItem.Checked = !tableToolStripMenuItem.Checked;
            viewToolStripMenuItem.Checked = !tableToolStripMenuItem.Checked;
            storedProcedureToolStripMenuItem.Checked = !tableToolStripMenuItem.Checked;
            if (tableToolStripMenuItem.Checked)
            {
                Options.ObjectType = Enums.SqlObjectType.Table;
            }
            OnChange();
        }

        private void viewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tableToolStripMenuItem.Checked = !viewToolStripMenuItem.Checked;
            objectTypeAllToolStripMenuItem.Checked = !viewToolStripMenuItem.Checked;
            storedProcedureToolStripMenuItem.Checked = !viewToolStripMenuItem.Checked;
            if (viewToolStripMenuItem.Checked)
            {
                Options.ObjectType = Enums.SqlObjectType.View;
            }
            OnChange();
        }

        private void storedProcedureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tableToolStripMenuItem.Checked = !storedProcedureToolStripMenuItem.Checked;
            viewToolStripMenuItem.Checked = !storedProcedureToolStripMenuItem.Checked;
            objectTypeAllToolStripMenuItem.Checked = !storedProcedureToolStripMenuItem.Checked;
            if (storedProcedureToolStripMenuItem.Checked)
            {
                Options.ObjectType = Enums.SqlObjectType.StoredProcedure;
            }
            OnChange();
        }

       
    }
}
